﻿<?php require_once('Connections/satphp.php'); ?>
<?php
function erro(){include "error-page.html";exit;} 

$idaluno = 3;

if (!isset($_GET['idturma'],$_GET['post']) || $_GET['idturma'] == '' || $_GET['post'] == '')erro();
else { $idturma = $_GET['idturma']; $idforum = $_GET['post']; }

if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_satphp, $satphp);
$query_Recordset1 = "SELECT * FROM forum LEFT JOIN turma ON(forum.idturma = turma.idturma) WHERE  forum.idforum = $idforum";
$Recordset1 = mysql_query($query_Recordset1, $satphp) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

mysql_select_db($database_satphp, $satphp);
$query_Recordset2 = "SELECT * FROM forum LEFT JOIN turma ON(forum.idturma = turma.idturma) WHERE  forum.idpai = $idforum";
$Recordset2 = mysql_query($query_Recordset2, $satphp) or die(mysql_error());
$row_Recordset2 = mysql_fetch_assoc($Recordset2);
$totalRows_Recordset2 = mysql_num_rows($Recordset2);


if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO forum (idturma, idaluno, titulo, mensagem, idpai) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($idturma, "int"),
                       GetSQLValueString($idaluno, "int"),
                       GetSQLValueString("RE - ".$row_Recordset1['titulo'], "text"),
                       GetSQLValueString($_POST['mensagem'], "text"),
                       GetSQLValueString($_POST['idpai'], "int"));

  mysql_select_db($database_satphp, $satphp);
  $Result1 = mysql_query($insertSQL, $satphp) or die(mysql_error());
  header("location: ?idturma=$idturma&post=$idforum");
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title><?php echo $row_Recordset1['titulo']; ?> | SATPHP </title>
<style type="text/css">
	table {
	width:500px; 
	margin-left: auto;
	margin-right: auto;
	border:1px solid #066;
	}
	td { border:1px solid #03F; }
	table #topo { background-color:#CCC }
	
</style>
</head>

<body>
<form action="?<?php echo "idturma=".$idturma."&post=".$idforum; ?>" method="post" name="form1" id="form1">
<table>
  <?php do { ?>
    <tr>
      <!-- <td><?php echo $row_Recordset1['idforum']; ?></td> -->
      <!-- <td><?php echo $row_Recordset1['idturma']; ?></td> -->
      <!-- <td><?php echo $row_Recordset1['idaluno']; ?></td> -->
      <td>ALUNO</td>
      <td id="topo"><?php echo $row_Recordset1['titulo']; ?></td>
      <td id="topo"><?php echo $row_Recordset1['datapostagem']; ?></td>
    <tr>
      <td colspan="3"><?php echo $row_Recordset1['mensagem']; ?></td>
    </tr>
    <!-- <td><?php echo $row_Recordset1['idpai']; ?></td> -->
    <!-- <td><?php echo $row_Recordset1['idturma']; ?></td> -->
    <!-- <td><?php echo $row_Recordset1['nome']; ?></td> -->
  </tr>
  <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>	
	  <?php if ($row_Recordset2 > 1){ do { ?>
        <tr>
        	<td>ALUNO</td>
       	  <td><?php echo $row_Recordset2['titulo']; ?></td>
          <td><?php echo $row_Recordset2['datapostagem']; ?></td>
        <tr>
        	<td colspan="3"><?php echo $row_Recordset2['mensagem']; ?></td>
        </tr>
        </tr>
        <?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); }; ?>
    <tr>
    	<td colspan="3" id="coment" >
		<textarea name="mensagem" style="width:99%" ></textarea>
        <input type="hidden" name="idpai" value="<?php echo $idforum; ?>" />
        <input type="hidden" name="MM_insert" value="form1" /></td>
    </tr>
    <tr>
		<td colspan="3" id="coment" align="right"><input type="submit" value="comentar" /></td>
    </tr>
</table>
</form>
</body>
</html>
<?php
mysql_free_result($Recordset1);

mysql_free_result($Recordset2);
?>